package com.mo.ld.activity.view

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ObjectAnimator
import android.os.Bundle
import android.view.animation.LinearInterpolator
import com.mo.ld.databinding.ActDashboardviewBinding
import com.mo.lib.ui.activity.BaseVbActivity
import java.util.*

/**
 * @ author：mo
 * @ data：2022/3/25:14:57
 * @ 功能：
 */
class ViewsActivity : BaseVbActivity<ActDashboardviewBinding>() {
    private var isAnimFinished = true
    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        viewBinding.dv1.setOnClickListener {
            viewBinding.dv1.setRealTimeValue(Random().nextInt(100))
        }
        viewBinding.dv2.setOnClickListener {
            viewBinding.dv2.setCreditValueWithAnim(Random().nextInt(950 - 350) + 350)
        }
        viewBinding.dv2.setCreditValueWithAnim(Random().nextInt(600) + 350)
        viewBinding.dv3.setOnClickListener {
            viewBinding.dv3.setCreditValue(Random().nextInt(950 - 350) + 350)
        }
        viewBinding.dv4.setOnClickListener {
            if (isAnimFinished) {
                val animator = ObjectAnimator.ofInt(viewBinding.dv4, "mRealTimeValue", viewBinding.dv4.getVelocity(), Random().nextInt(180))
                animator.setDuration(1500).interpolator = LinearInterpolator()
                animator.addListener(object : AnimatorListenerAdapter() {
                    override fun onAnimationStart(animation: Animator) {
                        isAnimFinished = false
                    }

                    override fun onAnimationEnd(animation: Animator) {
                        isAnimFinished = true
                    }

                    override fun onAnimationCancel(animation: Animator) {
                        isAnimFinished = true
                    }
                })
                animator.addUpdateListener { animation ->
                    val value = animation.animatedValue as Int
                    viewBinding.dv4.setVelocity(value)
                }
                animator.start()
            }
        }

    }

    override fun initData() {
    }


}